<template>
  <div>
    <div class="app-container">
      <div class="search-container">
        <el-form :inline="true" :model="sheepParturition" class="demo-form-inline">
          <el-form-item label="分娩日期">
            <el-date-picker v-model="sheepParturition.startChildbirthDate" type="date" placeholder="Pick a date"
              clearable />-
            <el-date-picker v-model="sheepParturition.endChildbirthDate" type="date" placeholder="Pick a date"
              clearable />
          </el-form-item>
          <el-form-item label="种母耳号">
            <el-input v-model="sheepParturition.eartag" clearable />
          </el-form-item>
          <el-form-item label="转出栋舍">
            <el-select v-model="sheepParturition.buildingId" style="width: 120px;" clearable>
              <el-option label="空胎" :value="1" />
              <el-option label="返情" :value="2" />
              <el-option label="流产" :value="3" />
              <el-option label="怀孕" :value="4" />
              <el-option label="未孕" :value="5" />
            </el-select>
          </el-form-item>
          <el-form-item label="转入栋舍">
            <el-select v-model="sheepParturition.toBuildingId" style="width: 120px;" clearable>
              <el-option label="未知" :value="1" />
              <el-option label="1" :value="2" />
              <el-option label="2" :value="3" />
              <el-option label="3" :value="4" />
              <el-option label="4" :value="5" />
              <el-option label="5" :value="5" />
            </el-select>
          </el-form-item>
          <el-form-item label="窝号">
            <el-select v-model="sheepParturition.broodNumber" style="width: 120px;" clearable>
              <el-option label="未知" :value="1" />
              <el-option label="1" :value="2" />
              <el-option label="2" :value="3" />
              <el-option label="3" :value="4" />
              <el-option label="4" :value="5" />
              <el-option label="5" :value="5" />
            </el-select>
          </el-form-item>

          <el-form-item label="负责人" prop="buyer">
            <el-select v-model="sheepParturition.userId" clearable style="width: 150px">
              <el-option label="刘永刚" :value="1" />
              <el-option label="孟盘龙" :value="2" />
              <el-option label="赵秋实" :value="3" />
              <el-option label="牛骞若" :value="4" />
              <el-option label="赵思彤" :value="5" />
              <el-option label="于晋哲" :value="6" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getSheepParturition()">查询</el-button>
          </el-form-item>
        </el-form>
        <div class="button-container">
          <el-button type="danger" @click="toAddDelivery()"><el-icon>
              <Plus />
            </el-icon>&nbsp;新增</el-button>
          <el-button :disabled="true" type="danger"><el-icon>
              <Plus />
            </el-icon>&nbsp;删除</el-button>
          <el-button :disabled="true" type="danger"><el-icon>
              <Plus />
            </el-icon>&nbsp;审核</el-button>
        </div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="childbirthId" label="序号" />
          <el-table-column prop="earTag" label="种母耳号" />
          <el-table-column prop="childbirthDate" label="分娩日期" />
          <el-table-column prop="gestationDays" label="妊娠天数" />
          <el-table-column prop="difficultyDelivery" label="分娩难易">
            <template #default="scope">
              <span v-if="scope.row.difficultyDelivery == 1">顺产</span>
              <span v-else-if="scope.row.difficultyDelivery == 2">难产</span>
              <span v-else-if="scope.row.difficultyDelivery == 3">助产</span>
            </template>
          </el-table-column>
          <el-table-column prop="motherHood" label="母性">
            <template #default="scope">
              <span v-if="scope.row.motherHood == 1">1分</span>
              <span v-else-if="scope.row.motherHood == 2">2分</span>
              <span v-else-if="scope.row.motherHood == 3">3分</span>
            </template>
          </el-table-column>
          <el-table-column prop="lactation" label="泌乳">
            <template #default="scope">
              <span v-if="scope.row.lactation == 1">1分</span>
              <span v-else-if="scope.row.lactation == 2">2分</span>
              <span v-else-if="scope.row.lactation == 3">3分</span>
            </template>
          </el-table-column>
          <el-table-column prop="mastitis" label="乳房炎">
            <template #default="scope">
              <span v-if="scope.row.mastitis == 1">1分</span>
              <span v-else-if="scope.row.mastitis == 2">2分</span>
              <span v-else-if="scope.row.mastitis == 3">3分</span>
            </template>
          </el-table-column>
          <el-table-column prop="broodWeight" label="窝总羔数" />
          <el-table-column prop="litterWeight" label="窝总重(公斤)" />
          <el-table-column prop="liveLambNum" label="活羔总数" />
          <el-table-column prop="liveLambWeight" label="活羔总重" />
          <el-table-column prop="liveLambAvgWeight" label="活羔均重" />
          <el-table-column prop="dieLambNum" label="死羔总数" />
          <el-table-column prop="dieLambWeight" label="死羔总重" />
          <el-table-column prop="dieLambAvgWeight" label="死羔均重" />
          <el-table-column prop="fatSheep" label="肥羊批次" />
          <el-table-column prop="healthyNumber" label="健羔数" />
          <el-table-column prop="lambNumber" label="弱羔数" />
          <el-table-column prop="malformationNumber" label="畸形数" />
          <el-table-column prop="stillbirthNumber" label="死胎数" />
          <el-table-column prop="freaksNumber" label="畸胎数" />
          <el-table-column prop="mummyNumber" label="木乃伊数" />
          <el-table-column prop="broodNumber" label="窝号" />
          <el-table-column prop="sex" label="性别">
            <template v-slot="scope">
              <el-tag v-if="scope.row.sex == 1" type="primary">公</el-tag>
              <el-tag v-if="scope.row.sex == 0" type="danger">母</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="buildingName" label="当前栋舍" />
          <el-table-column prop="columnName" label="当前栏位" />
          <el-table-column prop="toBuildingName" label="转入栋舍" />
          <el-table-column prop="toColumnName" label="转入栏位" />
          <el-table-column prop="userId" label="负责人">
            <template #default="scope">
              <span v-if="scope.row.userId == 1">刘永刚</span>
              <span v-else-if="scope.row.userId == 2">孟盘龙</span>
              <span v-else-if="scope.row.userId == 3">赵秋实</span>
              <span v-else-if="scope.row.userId == 4">牛骞若</span>
              <span v-else-if="scope.row.userId == 5">赵思彤</span>
              <span v-else-if="scope.row.userId == 6">于晋哲</span>
            </template>

          </el-table-column>
          <el-table-column prop="createName" label="创建人" />
          <el-table-column prop="createTime" label="创建时间" />
          <el-table-column prop="updateName" label="修改人" />
          <el-table-column prop="updateTime" label="修改时间" />
          <el-table-column prop="auditName" label="审核人" />
          <el-table-column prop="auditDate" label="审核时间" />
          <el-table-column prop="documentNumber" label="单据号" />
          <el-table-column fixed="right" label="操作" width="120px">
            <template #default="scope">
              <el-link type="primary" @click="deletepregnancys(scope.row.earTag)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>


        <el-pagination v-model:current-page="sheepParturition.pageIndex" v-model:page-size="sheepParturition.pageSize"
          :page-sizes="[10, 15, 20, 25]" :background="true" layout="total, sizes, prev, pager, next, jumper"
          :total="pageshow.totalCount" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import purcure from '@/api/PurchaseMent/index'
import sheepDeliverys from '@/api/Deliverys/index'
import { sheepdeliveryQuerys } from '@/api/Deliverys/model'
import { number } from 'echarts';
import { isNull } from 'lodash';
import { useRouter } from 'vue-router';

const router = useRouter()


//参数
const sheepParturition = reactive<sheepdeliveryQuerys>({
  "startChildbirthDate": "",
  "endChildbirthDate": "",
  "eartag": "",
  "buildingId": null,
  "toBuildingId": null,
  "broodNumber": "",
  "userId": null,
  "pageIndex": 1,
  "pageSize": 10
})


const pageshow = reactive({
  totalCount: 0,
  pageCount: 0
})

//种母分娩列表
const tableData: any = ref([])
const getSheepParturition = () => {
  sheepDeliverys.getList(sheepParturition).then((res: any) => {
    console.log('完整响应数据:', res);
    tableData.value = res.data.data,
      pageshow.totalCount = res.data.totalCount,
      pageshow.pageCount = res.data.pageCount
  })
}

//获取栋舍
const forms = reactive<buildingIdQuery>({
  "pageIndex": 1,
  "pageSize": 100,
  "ridgepoleTypeId": 0,
  "ridgepoleName": "",
  "responsiblePerson": ""
})
const buildingIdList: any = ref([])
const getBuildingIdList = () => {
  purcure.getrid(forms).then((res: any) => {
    buildingIdList.value = res.data.data
  })
}

//获取栏位
const columnIdList: any = ref([])
const getcolumnIdList = (id: any) => {
  console.log(id)
  purcure.getcolumn(id).then((res: any) => {
    columnIdList.value = res.data
    console.log(columnIdList.value)
  })
  columnIdList.value = []
}

//分页方法
const handleSizeChange = (val: number) => {
  sheepParturition.pageSize = val
  getSheepParturition();
}
const handleCurrentChange = (val: number) => {
  sheepParturition.pageIndex = val
  getSheepParturition();
}

//删除方法
const deletepregnancys = async (eartag: any) => {
  const eartags = reactive({
    eartag: eartag
  })
  ElMessageBox.confirm(
    '确定要删除吗?',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {

      sheepDeliverys.deleteparturition(eartags)
        .then(res => {
          if (res.data > 0) {
            ElMessage.success('删除成功')
            getSheepParturition()
          }
          else {
            ElMessage.error('删除失败')
          }
        })

    })

    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消操作',
      })
    })
}

//新增页面跳转
const toAddDelivery = () => {
  router.push({
    name: "AddDelivery",
  })
}

onMounted(() => {
  getSheepParturition()
  getBuildingIdList()
})

</script>
